function listNodeTool() {
    const debug = false;
    this.listNode = function (arr) {
        this.debug && console.log(`[listNode] arr is ${JSON.stringify(arr)}`);
        if (!arr || arr.length < 1) {
            return null;
        }

        let ret = {
            val: null,
            next: null,
        };
        let temp = ret;
        for (let i = 0; i < arr.length; i++) {
            temp.val = arr[i];
            temp.next = null;
            if (i < arr.length - 1) {
                temp.next = {
                    val: null,
                    next: null,
                };
            }

            temp = temp.next;
        }
        this.debug &&
            console.log(`[listNode complete] ret is ${JSON.stringify(ret)}`);
        return ret;
    };
    this.flat = function (listNode) {
        if (listNode == null) {
            return [];
        }

        let arr = [];
        let node = listNode;
        this.debug && console.log(`flat: node is ${JSON.stringify(node)}`);
        do {
            arr.push(node.val);
            node = node.next;
        } while (!!node && !!node.next);
        if (!!node) {
            arr.push(node.val);
        }

        this.debug &&
            console.log(`flat complete: arr is ${JSON.stringify(arr)}`);
        return arr;
    };
}
module.exports = listNodeTool;
